// components/component-tag-name.js
let pro_data = require('../../common/city/province.js')
let city_data = require('../../common/city/city.js')
let area_data = require('../../common/city/area.js')
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		showPicker: {
			type: Number
		},
		pickerValueDefault: {
			type: String
		}
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		pickerIndex: 0,
		provinceDataList: [],
		cityDataList: [],
		areaDataList: [],
		/* 是否显示控件 */
		showPicker: false,
		selects: [],
		path: '',
		titles: '',
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		showPicker() {
			this.setData({
				showPicker: true
			})
		},
		int_address() {
			var titles = '';
			if (this.data.pickerValueDefault != '') {
				var _path = this.data.pickerValueDefault.split('-0-')[1].split('-');
				var p_id = _path[0];
				for (var i = 0; i < pro_data.province.length; i++) {
					if (pro_data.province[i].id == p_id) {
						titles += pro_data.province[i].name;
						break;
					}
				}
				var c_id = _path[1];
				for (var i = 0; i < city_data.city.length; i++) {
					if (city_data.city[i].id == c_id) {
						titles += ',' + city_data.city[i].name;
						break;
					}
				}
				var a_id = _path[2];
				for (var i = 0; i < area_data.area.length; i++) {
					if (area_data.area[i].id == a_id) {
						titles += ',' + area_data.area[i].name;
						break;
					}
				}
				this.setData({
					path: this.data.pickerValueDefault,
					titles: titles
				})
				var _this = this;
				this.triggerEvent("getLabel", {
					name: _this.data.titles,
					path: _this.data.path
				})
			}
		},
		init() {
			this.setData({
				selects: [],
				path: '',
				cityDataList: [],
				areaDataList: [],
				pickerIndex: 0,
				provinceDataList: pro_data.province
			})
		},
		pickerBack() {
			if (this.data.pickerIndex == 0) {
				this.setData({
					showPicker: false
				})
			}
			if (this.data.pickerIndex == 1) {

				this.setData({
					pickerIndex: 0
				})
				this.data.selects.splice(this.data.selects.length - 1, 1)
			}
			if (this.data.pickerIndex == 2) {
				this.setData({
					pickerIndex: 1
				})
				this.data.selects.splice(this.data.selects.length - 1, 1)
			}
		},
		select_p(e) {
			var index = e.currentTarget.dataset.id;
			var o_arr = this.data.selects;
			o_arr.push(index);
			this.setData({
				selects: o_arr
			})
			var _id = this.data.provinceDataList[index].id;
			this.setData({
				path: this.data.provinceDataList[index].path + _id
			})
			var temp = [];
			for (var i = 0; i < city_data.city.length; i++) {
				if (city_data.city[i].pid == _id) {
					temp.push(city_data.city[i])
					this.setData({
						cityDataList: temp
					})
				}
			}
			this.setData({
				pickerIndex: 1
			})

		},
		select_c(e) {
			var index = e.currentTarget.dataset.id
			var o_arr = this.data.selects;
			o_arr.push(index);
			this.setData({
				selects: o_arr
			})
			var _id = this.data.cityDataList[index].id;
			this.setData({
				path: this.data.cityDataList[index].path + _id
			})
			var temp = []
			for (var i = 0; i < area_data.area.length; i++) {
				if (area_data.area[i].pid == _id) {
					temp.push(area_data.area[i])
					this.setData({
						areaDataList: temp
					})
				}
			}
			this.setData({
				pickerIndex: 2
			})
		},
		select_a(e) {
			var index = e.currentTarget.dataset.id
			var o_arr = this.data.selects;
			o_arr.push(index);
			this.setData({
				selects: o_arr
			})
			var _id = this.data.areaDataList[index].id;
			this.setData({
				path: this.data.areaDataList[index].path + _id,
				titles: this.data.provinceDataList[this.data.selects[0]].name + ',' +
					this.data.cityDataList[this.data.selects[1]].name + ',' +
					this.data.areaDataList[this.data.selects[2]].name,
				showPicker: false
			})
			var _this = this
			this.triggerEvent("getLabel", {
				name: _this.data.titles,
				path: _this.data.path
			})
		},
		pickerCancel() {
			this.setData({
				showPicker: false
			})
		},
		showPickerView() {
			this.setData({
				showPicker: true
			})
		},
	}

})
